package com.zhilianbao.leyaogo.view.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.utils.Utils;

/**
 * Author: hcy
 * Description: 一，二个按钮Dialog
 * Created: 2016/11/8 1512
 */
public class CommonDialog extends DialogFragment implements View.OnClickListener {

    private static final String KEY_MIDDLE = "middle", KEY_LEFT = "left", KEY_RIGHT = "right", KEY_CONTENT = "content",
            KEY_COUNT = "count",KEY_WARNING = "warning",KRY_LEFT_C = "left_color",KEY_RIGHT_C = "right_color";
    private String btnLeftStr, btnRightStr, btnMiddleStr, contentText,warningText;
    private int buttonCount;
    private TextView mContentTv,mWarningTv;
    private TextView mLeftRtv, mRightRtv, mMiddleRtv;
    private int leftTextColorRes,rightTextColorRes;

    private View mLine;

    /**
     * 获取一个按钮
     *
     * @param buttonText
     * @param contentText
     * @return
     */
    public static CommonDialog createSingleButton(String buttonText, String contentText) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_MIDDLE, buttonText);
        bundle.putInt(KEY_COUNT, 1);
        bundle.putString(KEY_CONTENT, contentText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取一个按钮
     *
     * @param buttonText
     * @param contentText
     * @return
     */
    public static CommonDialog createSingleButton(String buttonText, String contentText,String warningText) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_MIDDLE, buttonText);
        bundle.putInt(KEY_COUNT, 1);
        bundle.putString(KEY_CONTENT, contentText);
        bundle.putString(KEY_WARNING, warningText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个，默认右边确定，左边取消，设置左右两边颜色
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String contentText,int leftTextColorRes,int rightTextColorRes) {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        bundle.putInt(KRY_LEFT_C, leftTextColorRes);
        bundle.putInt(KEY_RIGHT_C, rightTextColorRes);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个，获取两个，默认右边确定，左边取消，默认左右两边颜色
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String contentText) {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个，设置文字，文字颜色
     *
     * @param leftButtonText
     * @param rightButtonText
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String leftButtonText, String rightButtonText, String contentText,int leftTextColorRes,int rightTextColorRes) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_LEFT, leftButtonText);
        bundle.putString(KEY_RIGHT, rightButtonText);
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        bundle.putInt(KRY_LEFT_C, leftTextColorRes);
        bundle.putInt(KEY_RIGHT_C, rightTextColorRes);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个，设置文字，文字颜色
     *
     * @param leftButtonText
     * @param rightButtonText
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String leftButtonText, String rightButtonText, String contentText,int leftTextColorRes,int rightTextColorRes,String warningText) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_LEFT, leftButtonText);
        bundle.putString(KEY_RIGHT, rightButtonText);
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        bundle.putInt(KRY_LEFT_C, leftTextColorRes);
        bundle.putInt(KEY_RIGHT_C, rightTextColorRes);
        bundle.putString(KEY_WARNING, warningText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个，默认右边确定，左边取消，默认左右两边颜色
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String contentText,String warningText) {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        bundle.putString(KEY_WARNING, warningText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个,显示 提示文字
     *
     * @param leftButtonText
     * @param rightButtonText
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String leftButtonText, String rightButtonText, String contentText,String warningText) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_WARNING, warningText);
        bundle.putString(KEY_LEFT, leftButtonText);
        bundle.putString(KEY_RIGHT, rightButtonText);
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    /**
     * 获取两个，默认文字颜色
     *
     * @param leftButtonText
     * @param rightButtonText
     * @param contentText
     * @return
     */
    public static CommonDialog createTwoButton(String leftButtonText, String rightButtonText, String contentText) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_LEFT, leftButtonText);
        bundle.putString(KEY_RIGHT, rightButtonText);
        bundle.putInt(KEY_COUNT, 2);
        bundle.putString(KEY_CONTENT, contentText);
        CommonDialog dialog = new CommonDialog();
        dialog.setArguments(bundle);
        return dialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        buttonCount = bundle.getInt(KEY_COUNT);
        warningText = bundle.getString(KEY_WARNING);
        if (buttonCount == 1) {
            btnMiddleStr = bundle.getString(KEY_MIDDLE);
        } else {
            btnLeftStr = bundle.getString(KEY_LEFT,getString(R.string.cancel));
            btnRightStr = bundle.getString(KEY_RIGHT,getString(R.string.confirm));
        }
        leftTextColorRes = bundle.getInt(KRY_LEFT_C,0);
        rightTextColorRes = bundle.getInt(KEY_RIGHT_C,0);
        contentText = bundle.getString(KEY_CONTENT);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_commnon, null);
        dialog.setContentView(view);
        dialog.getWindow().setLayout((int) (Utils.getScreenWidth() * 0.68), WindowManager.LayoutParams.WRAP_CONTENT);
        if (buttonCount == 2) {
            dialog.setCanceledOnTouchOutside(false);
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
        initView(view);
        return dialog;
    }

    private void initView(View view) {
        mWarningTv = (TextView) view.findViewById(R.id.tv_warning);
        mContentTv = (TextView) view.findViewById(R.id.tv_content);
        mLeftRtv = (TextView) view.findViewById(R.id.tv_left);
        mRightRtv = (TextView) view.findViewById(R.id.tv_right);
        mMiddleRtv = (TextView) view.findViewById(R.id.tv_middle);
        mLine = view.findViewById(R.id.line);
        mLeftRtv.setOnClickListener(this);
        mRightRtv.setOnClickListener(this);
        mMiddleRtv.setOnClickListener(this);
        if(rightTextColorRes!=0){
            mRightRtv.setTextColor(rightTextColorRes);
        }
        if(leftTextColorRes!=0){
            mLeftRtv.setTextColor(leftTextColorRes);
        }
        setButtonCountAndText();
        setContentText();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                if (mListener != null) {
                    mListener.left();
                }
                break;
            case R.id.tv_right:
                if (mListener != null) {
                    mListener.right();
                }
                break;
            case R.id.tv_middle:
                if (mListener != null) {
                    mListener.middle();
                }
                break;
        }
    }

    /**
     * 设置显示的按钮个数1-2
     */
    private void setButtonCountAndText() {
        if (buttonCount == 1) {
            mLeftRtv.setVisibility(View.GONE);
            mRightRtv.setVisibility(View.GONE);
            mMiddleRtv.setVisibility(View.VISIBLE);
            mLine.setVisibility(View.GONE);
            mMiddleRtv.setText(btnMiddleStr);
        } else {
            mLeftRtv.setVisibility(View.VISIBLE);
            mRightRtv.setVisibility(View.VISIBLE);
            mMiddleRtv.setVisibility(View.GONE);
            mLeftRtv.setText(btnLeftStr);
            mRightRtv.setText(btnRightStr);
        }
        if(CheckUtils.isEmpty(warningText)){
            mWarningTv.setVisibility(View.GONE);
        }else {
            mWarningTv.setVisibility(View.VISIBLE);
            mWarningTv.setText(warningText);
        }
    }


    /**
     * 内容
     */
    private void setContentText() {
        mContentTv.setText(contentText);
    }

    public interface OnButtonClickListener {
        void left();

        void right();

        void middle();
    }

    private OnButtonClickListener mListener;

    public void setOnButtonClickListener(OnButtonClickListener listener) {
        mListener = listener;
    }

}
